package com.csii.adhoc.dataready.serviceImpl;

import com.csii.adhoc.dataready.TDataConstant;
import com.csii.adhoc.dataready.TDataRelationPo;
import com.csii.adhoc.dataready.mapper.TDataRelationMapper;
import com.csii.adhoc.dataready.service.TDataRelationService;
import com.csii.adhoc.exception.ParamValidityException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @description: 数据集接口实现类
 * @author: zyliu
 * @create: 2021-01-13 09:32
 */
@Service("tDataRelationService")
@Slf4j
public class TDataRelationServiceImpl implements TDataRelationService {

    @Resource
    TDataRelationMapper tDataRelationMapper;

    @Override
    @Transactional
    public void tDataRelationMaintain(TDataRelationPo tDataRelationPo, String optMode) {
        //检查上传的值

        switch (optMode) {
            case TDataConstant.OPT_MODE_ADD:
                insertOneTDataRelation(tDataRelationPo);
                break;
            case TDataConstant.OPT_MODE_UPDATE:
                updateTDataRelationByPrimaryKey(tDataRelationPo);
                break;
            case TDataConstant.OPT_MODE_DELETE:
                deleteByRelationId(tDataRelationPo.getRelationId());
                break;
            default:
                log.error("optMode值不合法:" + optMode);
                throw new ParamValidityException("操作标志不合法:" + optMode);
        }
    }

    @Transactional
    @Override
    public void insertOneTDataRelation(TDataRelationPo tDataRelationPo) {
        if (StringUtils.isEmpty(tDataRelationPo.getModelId())) {
            tDataRelationPo.setModelId(UUID.randomUUID().toString().replace("-", ""));
        }
        tDataRelationMapper.insertOneTDataRelation(tDataRelationPo);
    }

    @Transactional
    @Override
    public int updateTDataRelationByPrimaryKey(TDataRelationPo tDataRelationPo) {
        return tDataRelationMapper.updateByPrimaryKey(tDataRelationPo);
    }

    @Transactional
    @Override
    public int deleteTDataRelationByPrimaryKey(String modelId) {
        return tDataRelationMapper.deleteByModelId(modelId);
    }

    @Transactional
    public int deleteByRelationId(String relationId) {
        return tDataRelationMapper.deleteByRelationId(relationId);
    }

    @Override
    public List<TDataRelationPo> selectListTDataRelation(String modelId) {
        return tDataRelationMapper.selectListTDataRelation(modelId);
    }

    @Override
    public List<Map<String,String>> selectTableIdByModelId(String modelId) {
        return tDataRelationMapper.selectTableIdByModelId(modelId);
    }

    @Override
    public TDataRelationPo selectOneTDataRelation(String modelId,String fldEnname) {
        return tDataRelationMapper.selectOneTDataRelation(modelId,fldEnname);
    }
    @Override
    public Map selCustDataDbTable(String sql){
        return tDataRelationMapper.selCustDataDbTable(sql);
    }
}
